home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_075 / comm / serialio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  11KB  |  408 lines

  1.  
  2. /* Comm serial I/O handlers */
  3.  
  4. extern struct IOExtSer *AllocMem();
  5. extern struct MenuItem BaudItems[];
  6. extern UBYTE  doinit[], doexit[];
  7.  
  8. static   int   bits, length, parity;
  9. static   int   xbits, xlength, xparity;
  10.  
  11. #define  SERIALIO 1
  12. #include "globals.h"
  13.  
  14. init_serial()
  15. {
  16.     Read_Request = AllocMem((long)sizeof(*Read_Request),
  17.                                   MEMF_PUBLIC | MEMF_CLEAR);
  18.     Read_Request->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  19.     Read_Request->IOSer.io_Message.mn_ReplyPort = CreatePort("Read_RS",0L);
  20.  
  21.     if(OpenDevice(SERIALNAME,0L,Read_Request,0L))
  22.        return FALSE;
  23.  
  24.     Read_Request->IOSer.io_Command = CMD_READ;
  25.     Read_Request->IOSer.io_Length  = 1;
  26.     Read_Request->IOSer.io_Data    = (APTR) &rs_in[0];
  27.  
  28.     Write_Request = AllocMem((long)sizeof(*Write_Request),
  29.                                    MEMF_PUBLIC | MEMF_CLEAR);
  30.     Write_Request->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  31.     Write_Request->IOSer.io_Message.mn_ReplyPort = CreatePort("Write_RS",0L);
  32.  
  33.     if(OpenDevice(SERIALNAME,0L,Write_Request,0L))
  34.       return FALSE;
  35.  
  36.     Echo_Request = AllocMem((long)sizeof(*Echo_Request),
  37.                         MEMF_PUBLIC | MEMF_CLEAR);
  38.     Echo_Request->io_SerFlags = SERF_XDISABLED | SERF_SHARED | IOF_QUICK;
  39.     Echo_Request->IOSer.io_Message.mn_ReplyPort = CreatePort("Echo_RS",0L);
  40.  
  41.     if(OpenDevice(SERIALNAME,0L,Echo_Request,0L))
  42.       return FALSE;
  43.  
  44.     Write_Request->IOSer.io_Command = CMD_WRITE;
  45.     Write_Request->IOSer.io_Length  = 1;
  46.     Write_Request->IOSer.io_Data    = (APTR) &rs_out[0];
  47.  
  48.     Echo_Request->IOSer.io_Command  = CMD_WRITE;
  49.     Echo_Request->IOSer.io_Length   = 1;
  50.     Echo_Request->IOSer.io_Data     = (APTR) &rs_echo[0];
  51.  
  52.     Read_Request->io_SerFlags       = SERF_SHARED | SERF_XDISABLED;
  53.     Read_Request->IOSer.io_Length   = 1;
  54.     Read_Request->IOSer.io_Data     = (APTR) &rs_in[0];
  55.     Read_Request->io_Baud           = baud;
  56.     Read_Request->io_ReadLen        = 8;
  57.     Read_Request->io_WriteLen       = 8;
  58.     Read_Request->io_CtlChar        = 0x11130000L;
  59.     Read_Request->io_RBufLen        = 1024;
  60.     Read_Request->IOSer.io_Command  = SDCMD_SETPARAMS;
  61.  
  62.     DoIO(Read_Request);
  63.     Read_Request->IOSer.io_Command  = CMD_READ;
  64.  
  65.     return TRUE;
  66. }
  67. /**************************************************************/
  68. /* send char and read char functions for the xmodem function */
  69. /************************************************************/
  70. sendchar(ch)
  71. int ch;
  72. {
  73.    rs_out[0] = ch & 0xFF;
  74.    DoIO(Write_Request);
  75. }
  76.  
  77.  
  78. /************************/
  79. /* echo char to sender  */
  80. /************************/
  81. echochar(ch)
  82. int ch;
  83. {
  84.    rs_echo[0] = ch & 0xFF;
  85.    DoIO(Echo_Request);
  86.    if(ch == '\r')
  87.      echochar('\n');
  88. }
  89.  
  90.  
  91. /*************************************
  92. readchar() waits for a character
  93.   timeout built in.
  94.   Timeout condition causes error return
  95.   'seconds' controls duration of wait.
  96.   'doabort' TRUE indicates that CAN recvd
  97.      will set cancel flag
  98. **************************************/
  99. readchar( seconds, doabort )
  100. unsigned int doabort, seconds;
  101. {
  102.    extern void emits_rx();
  103.    extern long CheckIO();
  104.  
  105.    UBYTE ch;
  106.  
  107.    timeout = FALSE;
  108.    StartTimer((unsigned long)seconds,0L);      /* start timeout */
  109.  
  110. /* wait for a TX window event, the timer to timeout or for a receive character */
  111. wait:
  112.    Wait(  1L << Read_Request->IOSer.io_Message.mn_ReplyPort->mp_SigBit
  113.         | 1L << TimerMsgPort.mp_SigBit
  114.         | 1L << tx_window->UserPort->mp_SigBit );
  115.  
  116.    if(CheckIO(Read_Request))     /* if IO request has completed */
  117.     {
  118.        AbortIO(&TimerIO);
  119.        Wait( 1L << TimerMsgPort.mp_SigBit);
  120.        WaitIO(Read_Request);     /* remove the message from the queue */
  121.        ch = rs_in[0];            /* get character */
  122.        BeginIO(Read_Request);    /* start a new request */
  123.        if( ch == CAN && doabort) /* if this is a CAN char and doabort TRUE */
  124.             cancel = TRUE;       /* set cancel flag */
  125.        return (int)(ch);
  126.     }
  127. /* see if the timer timed out */
  128.   if(CheckIO(&TimerIO))          /* was timeout */
  129.   {
  130.     emits_rx("  Timeout\n");
  131.     timeout = TRUE;
  132.     return TIMEOUT;
  133.   }
  134. /* otherwise it was a tx_window input */
  135.   Process_window_event();
  136.   if(abort)
  137.   {
  138.     AbortIO(&TimerIO);
  139.     Wait( 1L << TimerMsgPort.mp_SigBit);
  140.     return TIMEOUT;
  141.   }
  142.   goto wait;                     /* wait for Timeout or character */
  143. }
  144.  
  145. /***********************************
  146.   Flush receive buffer.
  147.   Removes pending rx characters to
  148.   help maintain line syncronization
  149. ************************************/  
  150. void purge_line()
  151. {
  152.    purge_port();
  153.    BeginIO(Read_Request);
  154. }
  155.  
  156. purge_port()
  157. {
  158.    AbortIO(Read_Request);                 /* abort any reads */
  159.    WaitIO(Read_Request);                  /* wait for abort to complete */
  160.  
  161.    Read_Request->IOSer.io_Command = CMD_FLUSH;
  162.    DoIO(Read_Request);                       /* flush all IO requests */
  163.  
  164.    Read_Request->IOSer.io_Command = CMD_CLEAR;
  165.    DoIO(Read_Request);                       /* flush receive buffer  */
  166.  
  167.    Read_Request->IOSer.io_Command = CMD_READ;
  168. }
  169.  
  170. /* enable/disable XON/XOFF checking in the serial driver */
  171. Xon(val)
  172. int val;
  173. {
  174.    AbortIO(Read_Request);
  175.    WaitIO(Read_Request);
  176.  
  177.    if(val)        /* if TRUE, turn on XON/XOFF handling */
  178.       Read_Request->io_SerFlags &= ~SERF_XDISABLED;
  179.    else           /* disable XON/XOFF */
  180.       Read_Request->io_SerFlags |= SERF_XDISABLED;
  181.  
  182.     Read_Request->IOSer.io_Command  = SDCMD_SETPARAMS;
  183.     DoIO(Read_Request);
  184.  
  185.     Read_Request->IOSer.io_Command  = CMD_READ;
  186.     BeginIO(Read_Request);
  187. }
  188.  
  189. /*  Close the serial device */
  190. Close_Serial()
  191. {
  192.   if( Read_Request )          /* if open, close it */
  193.    {
  194.      CloseDevice( Read_Request );
  195.      DeletePort( Read_Request->IOSer.io_Message.mn_ReplyPort );
  196.      FreeMem( Read_Request, (long)sizeof(*Read_Request) );
  197.    }
  198.   if( Write_Request )         /* ditto */
  199.    {
  200.      CloseDevice( Write_Request );
  201.      DeletePort( Write_Request->IOSer.io_Message.mn_ReplyPort );
  202.      FreeMem( Write_Request, (long)sizeof(*Write_Request) );
  203.    }
  204.   if( Echo_Request )          /* ditto */
  205.    {
  206.      CloseDevice( Echo_Request );
  207.      DeletePort( Echo_Request->IOSer.io_Message.mn_ReplyPort );
  208.      FreeMem( Echo_Request, (long)sizeof(*Echo_Request) );
  209.    }
  210. }
  211.  
  212.  
  213. /***********************************
  214.   Start line.
  215.   Restart line if stopped with XOFF
  216. ************************************/  
  217. void Start_line()
  218. {
  219.    sendchar(XON);
  220.  /* use Echo_Request because it's not busy at the time this is called */
  221.    Echo_Request->IOSer.io_Command = CMD_START;
  222.    DoIO(Echo_Request);                       /* Restart line request */
  223. }
  224.  
  225. /***********************************
  226. Set baud rate
  227. ***********************************/
  228. void set_baud( num )
  229. int num;
  230. {
  231.    USHORT i,itemnum;
  232.  
  233.    switch( num )
  234.    {
  235.       case 300:  itemnum = 0; break;
  236.       case 1200: itemnum = 1; break;
  237.       case 2400: itemnum = 2; break;
  238.       case 4800: itemnum = 3; break;
  239.       case 9600: itemnum = 4; break;
  240.       case 19200:itemnum = 5; break;
  241.       default: return;
  242.    }
  243.    baud = num;
  244.  
  245.    purge_port();
  246.    Read_Request->io_Baud = (long)num;  /* set new baud rate */
  247.    Setparams();
  248.  
  249.    for(i = 0; i < RSMAX; i++)
  250.       BaudItems[ i ].Flags       &= ~CHECKED;  /* reset Checkmark */
  251.       BaudItems[ itemnum ].Flags |= CHECKED;   /* Check real baud rate */
  252. }
  253.  
  254. /* see if any characters are in the receive buffer */
  255. /* return FALSE if IO pending, TRUE if not */
  256. check_line()
  257. {
  258.    return(CheckIO(Read_Request));
  259. }
  260.  
  261. /* Read the characters buffered in the RX buffer.
  262.    First satisfy the 1 character read we started.
  263.    Then do an I/O query to see how many more characters are ready.
  264.    Then issue a Read request for these characters.
  265. */
  266. USHORT Read_RS()
  267. {
  268.    USHORT len;
  269.  
  270.    WaitIO(Read_Request);      /* read the 1st rx character */
  271.  
  272.    Echo_Request->IOSer.io_Command = SDCMD_QUERY;
  273.    DoIO(Echo_Request);
  274.  
  275.    dcd = Echo_Request->io_Status & 0x28;
  276.    len = Echo_Request->IOSer.io_Actual;  /* how many bytes in RX buffer */
  277.  
  278.    if(len != 0)
  279.    {
  280.       if(len > RSBUFSIZ-1)          /* lets not overflow our buffer */
  281.          len = RSBUFSIZ-1;
  282.       Read_Request->IOSer.io_Length = len;
  283.       Read_Request->IOSer.io_Data    = (APTR) &rs_in[1];
  284.       DoIO(Read_Request);           /* go get em */
  285.       Read_Request->IOSer.io_Length = 1;
  286.       Read_Request->IOSer.io_Data    = (APTR) &rs_in[0];
  287.    }
  288.    return (len + 1);
  289. }
  290.  
  291.  
  292. SendBreak()
  293. {
  294.  /* use Echo_Request because it's not busy at the time this is called */
  295.    Echo_Request->IOSer.io_Command  = SDCMD_BREAK;
  296.    Echo_Request->io_SerFlags      |= SERF_QUEUEDBRK;
  297.    DoIO(Echo_Request);                       /* Restart line request */
  298.    Echo_Request->io_SerFlags      &= ~SERF_QUEUEDBRK;
  299. }
  300.  
  301. Setparity(parnum)
  302. int parnum;
  303. {
  304.   WaitRequest();
  305.   switch( parnum )
  306.   {
  307.      case 0:            /* no parity */
  308.               Read_Request->io_SerFlags &= ~SERF_PARTY_ON;
  309.               break;
  310.      case 1:            /* odd parity */
  311.               Read_Request->io_SerFlags |= SERF_PARTY_ODD | SERF_PARTY_ON;
  312.               break;
  313.      case 2:            /* even parity */
  314.               Read_Request->io_SerFlags |= SERF_PARTY_ON;
  315.               Read_Request->io_SerFlags &= ~SERF_PARTY_ODD;
  316.               break;
  317.   }
  318.   parity = parnum;
  319.   Setparams();
  320. }
  321.  
  322. Setlength( len )
  323. int len;
  324. {
  325.     WaitRequest();
  326.     switch( len )
  327.     {
  328.        case 0:            /* 8 bits */
  329.                  Read_Request->io_ReadLen =
  330.                  Read_Request->io_WriteLen = 8;
  331.                  break;
  332.        case 1:            /* 7 bits */
  333.                  Read_Request->io_ReadLen =
  334.                  Read_Request->io_WriteLen = 7;
  335.                  break;
  336.     }
  337.     length = len;
  338.     Setparams();
  339. }
  340.  
  341. Setstopbits( num )
  342. int num;
  343. {
  344.    if( (Read_Request->io_ReadLen == 8) & ( num == 1 ) )
  345.    {
  346.       emits_rx("SERIAL DEVICE ERROR: can't have 2 stop bits with 8 bit word\n");
  347.       return;
  348.    }
  349.  
  350.    WaitRequest();
  351.    switch( num )
  352.    {
  353.       case 0:            /* 1 stop bit */
  354.                 Read_Request->io_StopBits = 1;
  355.                 break;
  356.       case 1:            /* 2 stop bits */
  357.                 Read_Request->io_StopBits = 2;
  358.                 break;
  359.    }
  360.    bits = num;
  361.    Setparams();
  362. }
  363.  
  364. Setparams()
  365. {
  366.    Read_Request->IOSer.io_Command = SDCMD_SETPARAMS;
  367.    DoIO(Read_Request);
  368.    Read_Request->IOSer.io_Command = CMD_READ;
  369.    BeginIO(Read_Request);
  370. }
  371.  
  372. WaitRequest()
  373. {
  374.    AbortIO(Read_Request);                  /* abort it */
  375.    WaitIO(Read_Request);                   /* wait for abort to finish */
  376. }
  377.  
  378. Xconfig( state )
  379. int state;
  380. {
  381.    if(state == FALSE)          /* restore state */
  382.    {
  383.       Setstopbits( xbits );
  384.       Setparity( xparity );
  385.       Setlength( xlength );
  386.       Xon(xonflg);
  387.    }
  388.    else
  389.    {
  390.       xbits = bits; xparity = parity ; xlength = length;
  391.       Setstopbits( 0 );    /* 1 stop bit */
  392.       Setparity( 0 );      /* no parity */
  393.       Setlength( 0 );      /* 8 bits */
  394.       Xon( FALSE );
  395.    }
  396. }
  397.  
  398. Modem_init()
  399. {
  400.    sendout(doinit);
  401. }
  402.  
  403. Modem_exit()
  404. {
  405.    sendout(doexit);
  406. }
  407.  
  408.